package com.angel.controller;

import com.angel.bean.EmployeeType;
import com.angel.bean.SysRoleMenu;
import com.angel.service.EmployeeTypeService;
import com.angel.service.OtherlogService;
import com.angel.service.SysRoleMenuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/u")
public class EmployeeTypeController {

    @Autowired
    private EmployeeTypeService employeeTypeService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private OtherlogService otherlogService;

    /***
     * 跳转岗位添加页
     * @param model
     * @return
     */

    @GetMapping("/type")
    public ModelAndView gettype(Model model, @RequestParam(value = "p", defaultValue = "1") String pageNo) {
        //根菜单
        int pn = 1;
        try {
            pn = Integer.valueOf(pageNo);
        } catch (Exception e) {

        }
        PageHelper.startPage(pn, 10);
        List<EmployeeType> list = employeeTypeService.findEmployeeType(null);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo", pageInfo);
        ModelAndView mv = new ModelAndView("employee-type", "menuModel", model);
        return mv;
    }

    @PostMapping("/atype")
    @ResponseBody
    public Object addType(EmployeeType employeeType) {
        int res = 0;
        Map<String, Object> map = new HashMap();
        //如果主键有值，则进行更新，否则进行添加
        if (employeeType.getEmTypeId() == null) {
            res = employeeTypeService.insertSelective(employeeType);
            if (res != 0) {
                map.put("data", "添加成功");
            } else {
                map.put("data", "添加失败");
            }
        } else {
            res = employeeTypeService.update(employeeType);
            if (res != 0) {
                map.put("data", "更新成功");
            } else {
                map.put("data", "更新失败");
            }
        }
        return map;
    }


    @PostMapping("/one")
    @ResponseBody
    public Object findType(@RequestParam("emTypeId") String emTypeId) {
        //根菜单
        int tyid = 0;
        try {
            tyid = Integer.valueOf(emTypeId);
        } catch (Exception e) {
            return null;
        }
        EmployeeType employeeType = new EmployeeType();
        employeeType.setEmTypeId(tyid);
        employeeType = employeeTypeService.findOneType(employeeType);
        if (employeeType == null) {
            return null;
        }
        return employeeType;
    }

    @PostMapping("/del")
    @ResponseBody
    public Object delType(@RequestParam("emTypeId") String emTypeId) {
        //根菜单
        int etyid = 0;
        try {
            etyid = Integer.valueOf(emTypeId);
        } catch (Exception e) {
            return null;
        }
        Map<String, Object> map = new HashMap();
        EmployeeType employeeType = new EmployeeType();
        employeeType.setEmTypeId(etyid);
        int res = employeeTypeService.delEmployeeType(employeeType);
        SysRoleMenu sysRoleMenu = new SysRoleMenu();
        sysRoleMenu.setEid(etyid);
        sysRoleMenuService.delSysRoleMenu(sysRoleMenu);
        if (res == 0) {
            map.put("data", "删除失败");
            return map;
        }
        map.put("data", "删除成功");
        return map;
    }

}
